package eu.runelord.programmers.t135469;

import java.awt.Component;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AgentSwing {

	int delay = 1000; // delay for 5 sec.
	int period = 5000; // repeat every 15 sec.

	Timer timer = new Timer();
	
	String komunikat = "";


	public static void main(String[] args) {
		AgentSwing as = new AgentSwing();
		as.timer.scheduleAtFixedRate(as.tt(), as.delay, as.period);
	}

	public void doIt() {
		JFrame frame = new JFrame("");
		if (komunikat.equals("")) {
			komunikat = "Brak komunikatu";
		} else {
			komunikat = "Komunikat ... ";
			int x = JOptionPane.showConfirmDialog((Component) frame,
					(Object) komunikat, "", JOptionPane.YES_OPTION);

			timer.scheduleAtFixedRate(tt(), delay, period);
			if (x == 0) { // wybrano yes
				System.out.println(x);
				timer.scheduleAtFixedRate(tt(), delay, period);
				timer.cancel();
				komunikat = "";
			}
		}
	}

	private TimerTask tt() {
		return new TimerTask() {
			public void run() {
				doIt();
			}
		};
	}
}
